package com.dykj.fireFighting.web.base.video;

import com.dykj.fireFighting.api.base.video.dto.VideoDto;
import com.dykj.fireFighting.api.base.video.entity.PartitionsVideo;
import com.dykj.fireFighting.api.base.video.entity.Video;
import com.dykj.fireFighting.api.base.video.service.IPartitionsVideoService;
import com.dykj.fireFighting.api.base.video.service.IVideoDeviceService;
import com.dykj.fireFighting.api.base.video.service.IVideoService;
import com.dykj.fireFighting.api.base.video.vo.VideoDeviceVo;
import com.dykj.fireFighting.api.base.video.vo.VideoPlayVo;
import com.dykj.fireFighting.api.base.video.vo.VideoVo;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.utils.UUIDUtils;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 监控设备管理接口
 *
 * @Author hxb
 * @Date 2021-06-25 08:42:12
 */
@Slf4j
@Api(value = "监控设备管理接口", tags = {"监控设备管理接口"})
@RestController
@RequestMapping("device/video")
public class VideoController {

    @DubboReference
    private IVideoService videoService;
    @DubboReference
    private IPartitionsVideoService iPartitionsVideoService;

    @DubboReference
    private IVideoDeviceService videoDeviceService;


    /**
     * 监控设备管理列表
     *
     * @return
     */
    @AutoLog(value = "监控设备管理-分页列表查询")
    @ApiOperation(value = "监控设备管理分页列表", notes = "监控设备管理分页列表")
    @GetMapping("/list")
    @RequiresPermissions("device/video/list")
    public CommonResult<PageVo<VideoVo>> queryVideoList(VideoDto videoDto) {
        LoginUserVo loginUserVo = ShiroUtil.getUser();
        if (StringUtils.isBlank(videoDto.getAreaCode())) {
            videoDto.setAreaCode(loginUserVo.getAreaCode());
        }
        PageVo<VideoVo> page = videoService.queryVideoList(videoDto, loginUserVo);
        return CommonResult.success(page);
    }

    /**
     * 监控设备管理列表
     *
     * @return
     */
    @AutoLog(value = "监控设备管理-分页列表查询")
    @ApiOperation(value = "监控设备管理分页列表", notes = "监控设备管理分页列表")
    @GetMapping("/positionList")
    //@RequiresPermissions("device/video/positionList")
    public CommonResult<List<VideoVo>> queryVideoPositionlist() {
        LoginUserVo loginUserVo = ShiroUtil.getUser();
        List<VideoVo> list= videoService.queryVideoPositionList(loginUserVo.getEnterpriseId().toString());
        return CommonResult.success(list);
    }

    /**
     * 获取监控设备管理信息
     */
    @AutoLog(value = "监控设备管理-获取监控设备管理信息")
    @ApiOperation(value = "获取监控设备管理信息", notes = "获取监控设备管理信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    @RequiresPermissions("device/video/info")
    public CommonResult<VideoDeviceVo> info(@PathVariable("id") String id) {
        Video video = videoService.getById(id);

        VideoDeviceVo videoDeviceVo=new VideoDeviceVo();
        BeanUtils.copyProperties(video,videoDeviceVo);
        List<String> device=videoDeviceService.getByVideoId(id,0);
        List<String> deviceTradtion=videoDeviceService.getByVideoId(id,1);
        videoDeviceVo.setDevice(device);
        videoDeviceVo.setDeviceTradition(deviceTradtion);

        return CommonResult.success(videoDeviceVo);
    }

    /**
     * 保存监控设备管理信息
     */
    @AutoLog(value = "监控设备管理-保存监控设备管理信息")
    @ApiOperation(value = "保存监控设备管理信息", notes = "保存监控设备管理信息")
    @PostMapping("/save")
    @RequiresPermissions("device/video/save")
    public CommonResult save(@Valid @RequestBody VideoDeviceVo video) {
        try {
            LoginUserVo userVo = ShiroUtil.getUser();
            video.setUnitId(Long.toString(userVo.getEnterpriseId()));
            video.setCreateTime(new Date());
            video.setCreateBy(Long.toString(userVo.getId()));
            videoService.insertVideo(video);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改监控设备管理信息
     */
    @AutoLog(value = "监控设备管理-修改监控设备管理信息")
    @ApiOperation(value = "修改监控设备管理信息", notes = "修改监控设备管理信息")
    @PostMapping("/update")
    @RequiresPermissions("device/video/update")
    public CommonResult update(@Valid @RequestBody VideoDeviceVo video) {
        try {
            LoginUserVo userVo = ShiroUtil.getUser();
            video.setUpdateTime(new Date());
            video.setUpdateBy(Long.toString(userVo.getId()));
            videoService.updateVideo(video);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除监控设备管理信息
     */
    @AutoLog(value = "监控设备管理-删除监控设备管理信息")
    @ApiOperation(value = "删除监控设备管理信息", notes = "删除监控设备管理信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    @RequiresPermissions("device/video/delete")
    public CommonResult delete(@RequestBody String[] ids) {
        try {
            videoService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 视频播放
     *
     * @param id
     * @return
     */
    @AutoLog(value = "视频播放")
    @ApiOperation(value = "视频播放", notes = "视频播放")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/play/{id}")
    //@RequiresPermissions("device/video/paly")
    public CommonResult<VideoPlayVo> play(@PathVariable("id") String id) {
        VideoPlayVo videoPlayVo = videoService.videoPlay(id);
        return CommonResult.success(videoPlayVo);
    }
    @AutoLog(value = "添加视频监控位置")
    @ApiOperation(value = "添加视频监控位置", notes = "添加视频监控位置")
    @PostMapping("/savePosition")
    public CommonResult savePosition(@Valid @RequestBody PartitionsVideo video)
    {
        LoginUserVo userVo = ShiroUtil.getUser();
        video.setUnitId(userVo.getEnterpriseId().toString());
        iPartitionsVideoService.savePosition(video);
        return CommonResult.success("success");
    }

    /**
     * 获取单位所有摄像头
     * @param unitId
     * @return
     */
    @GetMapping("/getVideoTree")
    public CommonResult getVideoTree(String unitId){
        return CommonResult.success(videoService.getVideoTree(unitId));
    }




}
